﻿using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace MyHomeworkWebAPi.Models.ef;

public partial class SecondHandTradingPlatformContext : DbContext
{
    public SecondHandTradingPlatformContext()
    {
    }

    public SecondHandTradingPlatformContext(DbContextOptions<SecondHandTradingPlatformContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Comment> Comments { get; set; }

    public virtual DbSet<Order> Orders { get; set; }

    public virtual DbSet<Product> Products { get; set; }

    public virtual DbSet<User> Users { get; set; }

    public virtual DbSet<Log> Logs { get; set; }

    public virtual DbSet<ProductCategory> ProductCategories { get; set; }

    public virtual DbSet<Interaction> Interactions { get; set; }

    public virtual DbSet<Message> Messages { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        // 移除硬编码的连接字符串，使用依赖注入的方式
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Comment>(entity =>
        {
            entity.HasKey(e => e.CommentId).HasName("PK__comments__E79576877142FE10");

            entity.ToTable("comments");

            entity.Property(e => e.CommentId)
                .ValueGeneratedNever()
                .HasComment("评论ID")
                .HasColumnName("comment_id");

            entity.Property(e => e.Content)
                .HasMaxLength(200)
                .HasColumnType("nvarchar(200)")
                .HasComment("评论")
                .HasColumnName("content");

            entity.Property(e => e.CreateTime)
                .HasDefaultValueSql("(getdate())")
                .HasComment("创建时间")
                .HasColumnType("datetime")
                .HasColumnName("create_time");

            entity.Property(e => e.ProductId)
                .HasComment("商品ID")
                .HasColumnName("product_id");

            entity.Property(e => e.UserId)
                .HasComment("用户ID")
                .HasColumnName("user_id");

            entity.Property(e => e.ParentCommentId)
                .HasComment("父评论ID")
                .HasColumnName("parentComment_id");

            entity.HasOne(d => d.Product).WithMany(p => p.Comments)
                .HasForeignKey(d => d.ProductId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_Comments_Products");

            entity.HasOne(d => d.ParentComment)
                .WithMany(p => p.Replies)
                .HasForeignKey(d => d.ParentCommentId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_Comments_ParentComment");
        });

        modelBuilder.Entity<Order>(entity =>
        {
            entity.HasKey(e => e.OrderId).HasName("PK__orders__4659622986B47BF5");

            entity.ToTable("orders");

            entity.Property(e => e.OrderId)
                .ValueGeneratedNever()
                .HasComment("订单ID")
                .HasColumnName("order_id");

            entity.Property(e => e.CreateTime)
                .HasDefaultValueSql("(getdate())")
                .HasComment("创建时间")
                .HasColumnType("datetime")
                .HasColumnName("create_time");

            entity.Property(e => e.ProductId)
                .HasComment("商品ID")
                .HasColumnName("product_id");

            entity.Property(e => e.Status)
                .HasMaxLength(50)
                .HasColumnType("nvarchar(50)")
                .HasComment("状态")
                .HasColumnName("status")
                .HasDefaultValue("待付款");

            entity.Property(e => e.UserId)
                .HasComment("买家ID")
                .HasColumnName("user_id");

            entity.HasOne(d => d.Product).WithMany(p => p.Orders)
                .HasForeignKey(d => d.ProductId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__orders__product___45F365D3");

            entity.HasOne(d => d.User).WithMany(p => p.Orders)
                .HasForeignKey(d => d.UserId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__orders__user_id__44FF419A");
        });

        modelBuilder.Entity<Product>(entity =>
        {
            entity.HasKey(e => e.ProductId).HasName("PK__Products__47027DF5FA6ED102");

            entity.ToTable("products");

            entity.Property(e => e.ProductId)
                .ValueGeneratedNever()
                .HasComment("商品ID")
                .HasColumnName("product_id");

            entity.Property(e => e.CategoryId)
                .HasComment("分类ID")
                .HasColumnName("category_id");

            entity.Property(e => e.Description)
                .HasMaxLength(200)
                .HasColumnType("nvarchar(200)")
                .HasComment("描述")
                .HasColumnName("description");

            entity.Property(e => e.Image)
                .HasColumnType("varbinary(max)")
                .HasComment("商品图片")
                .HasColumnName("image");

            entity.Property(e => e.Price)
                .HasComment("价格")
                .HasColumnType("decimal(10, 2)")
                .HasColumnName("price");

            entity.Property(e => e.Status)
                .HasMaxLength(50)
                .HasColumnType("nvarchar(50)")
                .HasComment("状态")
                .HasColumnName("status");

            entity.Property(e => e.Title)
                .HasMaxLength(100)
                .HasColumnType("nvarchar(100)")
                .HasComment("商品标题")
                .HasColumnName("title");

            entity.Property(e => e.UserId)
                .HasComment("发布者ID")
                .HasColumnName("user_id");

            entity.HasOne(d => d.User).WithMany(p => p.Products)
                .HasForeignKey(d => d.UserId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK__Products__user_i__3A81B327");

            entity.HasOne(d => d.Category).WithMany(p => p.Products)
                .HasForeignKey(d => d.CategoryId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_Products_Category");
        });

        modelBuilder.Entity<User>(entity =>
        {
            entity.ToTable("users");

            entity.Property(e => e.UserId)
                .ValueGeneratedNever()
                .HasComment("用户ID")
                .HasColumnName("user_id");

            entity.Property(e => e.Avatar)
                .HasMaxLength(50)
                .HasColumnType("nvarchar(50)")
                .HasComment("头像")
                .HasColumnName("avatar");

            entity.Property(e => e.Nickname)
                .HasMaxLength(50)
                .HasColumnType("nvarchar(50)")
                .HasComment("用户名")
                .HasColumnName("nickname");

            entity.Property(e => e.Password)
                .HasMaxLength(255)
                .HasColumnType("nvarchar(255)")
                .HasComment("密码")
                .HasColumnName("password");

            entity.Property(e => e.Phone)
                .HasMaxLength(15)
                .HasColumnType("nvarchar(15)")
                .HasComment("手机号")
                .HasColumnName("phone");

            entity.Property(e => e.StudentId)
                .HasMaxLength(20)
                .HasColumnType("nvarchar(20)")
                .HasComment("学号")
                .HasColumnName("student_id");
        });

        modelBuilder.Entity<ProductCategory>(entity =>
        {
            entity.HasKey(e => e.CategoryId).HasName("PK_ProductCategories");

            entity.ToTable("categories");

            entity.Property(e => e.CategoryId)
                .ValueGeneratedNever()
                .HasComment("分类ID")
                .HasColumnName("category_id");

            entity.Property(e => e.Name)
                .HasMaxLength(50)
                .HasColumnType("nvarchar(50)")
                .HasComment("分类名称")
                .HasColumnName("name");

            entity.Property(e => e.IsUse)
                .HasComment("是否使用")
                .HasColumnName("is_use");
        });

        modelBuilder.Entity<Interaction>(entity =>
        {
            entity.ToTable("interaction");

            entity.Property(e => e.InteractionId)
                .ValueGeneratedNever()
                .HasComment("互动行为id")
                .HasColumnName("interaction_id");

            entity.Property(e => e.UserId)
                .HasComment("用户id")
                .HasColumnName("user_id");

            entity.Property(e => e.ProductId)
                .HasComment("商品id")
                .HasColumnName("product_id");

            entity.Property(e => e.Type)
                .HasComment("类型（1：收藏；2：浏览）")
                .HasColumnName("type");

            entity.Property(e => e.CreateTime)
                .HasComment("时间")
                .HasColumnType("datetime")
                .HasColumnName("create_time");

            entity.HasOne(d => d.Product)
                .WithMany(p => p.Interactions)
                .HasForeignKey(d => d.ProductId)
                .OnDelete(DeleteBehavior.ClientSetNull)
                .HasConstraintName("FK_interaction_products");

            entity.HasOne(d => d.User)
                .WithMany(p => p.Interactions)
                .HasForeignKey(d => d.UserId)
                .HasConstraintName("FK_interaction_users");
        });

        modelBuilder.Entity<Message>(entity =>
        {
            entity.ToTable("messages");

            entity.HasIndex(e => e.UserId, "idx_messages_user_id");

            entity.Property(e => e.MessageId)
                .ValueGeneratedNever()
                .HasColumnName("message_id");

            entity.Property(e => e.UserId)
                .HasColumnName("user_id");

            entity.Property(e => e.Content)
                .HasColumnName("content");

            entity.Property(e => e.IsRead)
                .HasColumnName("is_read");

            entity.Property(e => e.CreateTime)
                .HasColumnType("datetime")
                .HasColumnName("create_time");

            entity.HasOne(d => d.User)
                .WithMany(p => p.Messages)
                .HasForeignKey(d => d.UserId)
                .HasConstraintName("fk_messages_user_id");
        });

        modelBuilder.Entity<Log>(entity =>
        {
            entity.ToTable("operation_log");

            entity.Property(e => e.Id)
                .ValueGeneratedNever()
                .HasComment("日志ID")
                .HasColumnName("id");

            entity.Property(e => e.UserId)
                .HasComment("用户ID")
                .HasColumnName("user_id");

            entity.Property(e => e.Detail)
                .HasMaxLength(500)
                .HasComment("日志详情")
                .HasColumnName("detail");

            entity.Property(e => e.CreateTime)
                .HasDefaultValueSql("(getdate())")
                .HasComment("创建时间")
                .HasColumnType("datetime")
                .HasColumnName("create_time");

            entity.HasOne(d => d.User)
                .WithMany()
                .HasForeignKey(d => d.UserId)
                .OnDelete(DeleteBehavior.SetNull)
                .HasConstraintName("FK_Logs_Users");
        });

        OnModelCreatingPartial(modelBuilder);
    }

    partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
}
